/*

Copyright (C) Hobby Coders, 2012 - 2013

Module Name:
   WEB.H v1.0 BETA Source Code

Author:
    Sriram.A.S. sriramdasty7@gmail.com

Release Date:
    23 Jan 2012

Notes:
    This API is used to create Web ServerSide Scripts using C
Check the documentation and examples of how to use this API
Visit the blog for detailed guide, more instructions and updates
http://mypersonalsoft.blogspot.com
NOT THROUGHLY TESTED, USE AT OWN RISK

*/
#ifndef __WEB_H
#define __WEB_H
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
char returnRawPostString_postData[1000];
int returnRawPostString_isCalled=0;
void initHtml(void)
{
printf("Content-type: text/html\r\n\n");
}
char* returnRawGetString(void)
{
return getenv("QUERY_STRING");
}
void unencode(char *src,char *dest)
{
//unencode() function idea courtesy:
//http://www.cs.tut.fi/~jkorpela/forms/cgic.html
for(;*src!='\0';src++,dest++)
if(*src=='+')
*dest=' ';
else if(*src=='%')
{
int code;
if(sscanf(src+1,"%2x",&code)!=1) code = '?';
*dest = code;
src +=2;
}     
else
*dest = *src;
*dest = '\0';
}
void commonParseLogic(char* string,char* name,char delimiter,char temp_delimiter,int allow_spaces,int only_isset,char* target)
{
char buff[1000];
int i=0;
int j=0;
int flag=0;
if(string==NULL || *(string+0)=='\0')
{
strcpy(target,"");
return;
}
while(*(string+i)!='\0')
{
if(flag==1)
{
if(allow_spaces==1)
{
while(*(string+i)=='%' && *(string+i+1)=='2' && *(string+i+2)=='0')
{
i+=3;
}
while(*(string+i)==' ')
{
++i;
}
}
j=0;
flag=0;
}
buff[j++]=*(string+i);
if(*(string+i)==temp_delimiter)
{
buff[j-1]='\0';
if(strcmp(buff,name)==0)
{
if(only_isset==1)
{
strcpy(target,"y");
return;
}
j=0;
while(1)
{
if(*(string+i)==temp_delimiter)
{
++i; continue;
}
if(*(string+i)==delimiter || *(string+i)=='\0')
break;
buff[j++]=*(string+i);
++i;
}
buff[j]='\0';
strcpy(target,buff);
return;
}
}
else if(*(string+i)==delimiter)
{
if(only_isset==1)
{
buff[j-1]='\0';
if(strcmp(buff,name)==0)
{
strcpy(target,"y");
return;
}
}
flag=1;
}
++i;
}
if(only_isset==1)
{
buff[j]='\0';
if(strcmp(buff,name)==0)
{
strcpy(target,"y");
return;
}
}
strcpy(target,"");
}
void setCookie(char* cookie_name,char* cookie_value,long cookie_life_seconds)
{
time_t seconds;
struct tm * timeinfo;
char str_time[80];
time(&seconds);
seconds+=cookie_life_seconds;
timeinfo=gmtime(&seconds);
strftime(str_time,80,"%d %b %Y %X",timeinfo);
printf("Set-Cookie: %s=%s; Expires=%s GMT\r\n",cookie_name,cookie_value,str_time);
}
void destroyCookie(char* name)
{
printf("Set-Cookie: %s=; Expires=06 Oct 1989 00:00:00 GMT\r\n",name);
}
char* returnRawCookieString(void)
{
return getenv("HTTP_COOKIE");
}
void returnCookieString(char* name,char* ptr)
{
commonParseLogic(returnRawCookieString(),name,';','=',1,0,ptr);
}
char* returnRawPostString(void)
{
extern char returnRawPostString_postData[1000];
extern int returnRawPostString_isCalled;
if((int)getenv("CONTENT_LENGTH")==0)
return "";
if(returnRawPostString_isCalled==0)
{
fgets(returnRawPostString_postData,(int)getenv("CONTENT_LENGTH"),stdin);
returnRawPostString_isCalled=1;
}
return returnRawPostString_postData;
}
void returnPostString(char* name,char* result)
{
if(returnRawPostString()=="")
{
strcpy(result,"");
}
else
{
commonParseLogic(returnRawPostString(),name,'&','=',0,0,result);
if(result!="" || result!=NULL)
{
unencode(result,result);
}
}
}
void returnGetString(char* name,char* result)
{
if(returnRawGetString()=="")
{
strcpy(result,"");
}
else
{
commonParseLogic(returnRawGetString(),name,'&','=',0,0,result);
if(result!="" || result!=NULL)
{
unencode(result,result);
}
}
}
void includeFile(char* filename)
{
FILE *fp=fopen(filename,"r");
char ch;
while(ch=getc(fp))
{
if(ch==EOF)
break;
printf("%c",ch);
}
fclose(fp);
}
void setHttpHeader(char* http_field,char* http_field_option)
{
printf("%s: %s\r\n",http_field,http_field_option);
}
void redirect(char* dest)
{
setHttpHeader("Location",dest);
}
int isset(char* type,char* name)
{
char ch[2]="n";
if(strcmp(type,"GET")==0)
{
commonParseLogic(returnRawGetString(),name,'&','=',0,1,ch);
}
else if(strcmp(type,"COOKIE")==0)
{
commonParseLogic(returnRawCookieString(),name,';','=',1,1,ch);
}
else if(strcmp(type,"POST")==0)
{
commonParseLogic(returnRawPostString(),name,'&','=',0,1,ch);
}
if(strcmp(ch,"y")==0)
{
return 1;
}
return 0;
}
#endif  /* __WEB_H */